/*
 * Date          Author       Note
 * 2015.06.23    舞者无罪              锁机
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <minigui/common.h>
#include <minigui/minigui.h>
#include <minigui/gdi.h>
#include <minigui/window.h>
#include <minigui/control.h>
#include "mh/common.h"
#include "resource.h"
#include "func.h"
#include "lang.h"
#include "mhbtn.h"
#include "mhedit.h"
#include "mhlabel.h"
#include "payment.h"
#include "com.h"
#include "hex_key.h"
#include "info.h"
#include "dat.h"


/*
 *
 */
void FlushPaymentWindowID(HWND hWnd)
{
    SYS_ID id;
    GetPaymentID(g_knit_data, &id);
    char sc[128] = { 0 };
    int i = 0;
    for(i = 0; i < sizeof(id.id); ++i)
    {
        sprintf(&sc[strlen(sc)], "%02X", id.id[i]);
        if((i & 1) && i != sizeof(id.id) - 1)
        {
            strcat(&sc[strlen(sc)], " ");
        }
    }
    HWND hTmp = GetDlgItem(hWnd, ID_EDIT);
    SetWindowCaption(hTmp, sc);
    InvalidateRect(hTmp, NULL, TRUE);
}
/*
 *
 */
static void CreatePaymentInterface(HWND hWnd)
{
    //sys btn
    CreateWindow(MH_STATIC_NAME, "", WS_CHILD | WS_VISIBLE, ID_ESC,
        CX_RAB_BMP - CX_ASK_BMP * 2, 10, CX_ASK_BMP, CY_ASK_BMP,
        hWnd, (int)&g_bmp[BMP_FALSE]);
    CreateWindow(MH_LABEL_NAME,
        GetString(788),
        WS_CHILD | WS_VISIBLE,
        IDC_STATIC,
        10,
        60,
        80,
        20,
        hWnd, MH_LABEL_BKGND_BLACK | MH_LABEL_TEXT_GREEN);
    SYS_ID id;
    GetPaymentID(g_knit_data, &id);
    char sc[128] = {0};
    int i = 0;
    for(i = 0; i < sizeof(id.id); ++i)
    {
        sprintf(&sc[strlen(sc)], "%02X", id.id[i]);
        if((i & 1) && i != sizeof(id.id) - 1)
        {
            strcat(&sc[strlen(sc)], " ");
        }
    }
    CreateWindow(MH_LABEL_NAME,
        sc,
        WS_CHILD | WS_VISIBLE | BS_LEFT,
        ID_EDIT,
        90,
        60,
        320,
        20,
        hWnd, MH_LABEL_BKGND_BLACK | MH_LABEL_TEXT_CYAN);
    CreateWindow(MH_BTN_NAME,
        GetString(797),
        WS_CHILD | WS_VISIBLE,
        ID_WRITE_PARAM,
        415,
        60,
        70,
        20,
        hWnd, 0);
    //
    CreateWindow(MH_LABEL_NAME,
        GetString(789),
        WS_CHILD | WS_VISIBLE,
        IDC_STATIC,
        10,
        100,
        80,
        20,
        hWnd, MH_LABEL_BKGND_BLACK | MH_LABEL_TEXT_GREEN);
    if(id.time_left < 720 * 24 * 3600)
    {
        int day = id.time_left / (24 * 3600);
        int hour = (id.time_left % 86400) / 3600;
        int min = (id.time_left % 3600) / 60;
        sprintf(sc, GetString(791), day, hour, min);
    }
    else
    {
        sc[0] = 0;
        strcat(sc, GetString(790));
    }
    CreateWindow(MH_LABEL_NAME,
        sc,
        WS_VISIBLE | WS_CHILD | BS_LEFT,
        ID_TIME,
        90,
        100,
        200,
        20,
        hWnd, MH_LABEL_BKGND_BLACK | MH_LABEL_TEXT_CYAN);
    //
    CreateWindow(MH_LABEL_NAME,
        GetString(792),
        WS_VISIBLE | WS_CHILD,
        IDC_STATIC,
        10,
        140,
        80,
        20,
        hWnd, MH_LABEL_BKGND_BLACK | MH_LABEL_TEXT_GREEN);
    CreateWindow(MH_EDIT_NAME,
        "",
        WS_CHILD | WS_VISIBLE,
        IDC_EDIT1,
        90,
        140,
        320,
        20,
        hWnd, MH_EDIT_BORDER_GREY);
    CreateWindow(MH_BTN_NAME,
        GetString(793),
        WS_CHILD | WS_VISIBLE,
        ID_READ_PARAM,
        415,
        140,
        70,
        20,
        hWnd, 0);
}
/*
 *
 */
static void OnBtnCmd(HWND hWnd, int nID)
{
    if(nID == ID_ESC)
    {
        PostMessage (hWnd, MSG_CLOSE, 0, 0);
    }
    else if(nID == IDC_EDIT1)
    {
        RECT rect;
        HWND hTmp = GetDlgItem(hWnd, nID);
        GetClientRect(hTmp, &rect);
        ClientToScreen(hTmp, &rect.left, &rect.top);
        ClientToScreen(hTmp, &rect.right, &rect.bottom);
        CreateHexKeyWindow(hWnd, rect);
    }
    else if(nID == ID_READ_PARAM)
    {
        HWND hTmp = GetDlgItem(hWnd, ID_EDIT);
        const char *psc = GetWindowCaption(hTmp);
        if(HasUsb())
        {
            char sc[128] = {0};
            char file[MH_FILE_NAME_MAX_LEN] = {0};
            strcat(sc, psc);
            Trim(sc);
            strcat(file, GetUsbDir());
            strcat(file, sc);
            strcat(file, ".txt");
            //printf("file: %s\n", file);
            if(ReadSNFile(file, sc, sizeof(sc)) == MH_FILE_ERROR_OPEN)
            {
                psc = GetString(794);
            }
            else if(SetPaymentID(g_knit_data, sc, sizeof(sc)))
            {
                psc = GetString(796);
            }
            else
            {
                psc = GetString(795);
                FlushPaymentWindowID(hWnd);
            }
        }
        else
        {
            psc = GetString(555);
        }
        AfxInfoBox(hWnd, NULL, psc);
    }
    else if(nID == ID_WRITE_PARAM)
    {
        char sc[128] = {0};
        HWND hTmp = GetDlgItem(hWnd, ID_EDIT);
        const char *psc = GetWindowCaption(hTmp);
        if(HasUsb())
        {
            char file[MH_FILE_NAME_MAX_LEN] = {0};
            strcat(sc, psc);
            Trim(sc);
            strcat(file, GetUsbDir());
            strcat(file, sc);
            strcat(file, ".cfg");
            if(WriteSNFile(file, sc, strlen(sc) + 1))
            {
                psc = GetString(481);
            }
            else
            {
                psc = GetString(482);
                MH_FILE_SYNC();
            }
        }
        else
        {
            psc = GetString(555);
        }
        AfxInfoBox(hWnd, NULL, psc);
    }
}
/*
 *
 */
static void OnKeyDown(HWND hWnd, uint key)
{
    if(key == SCANCODE_ESCAPE)
    {
        OnBtnCmd(hWnd, ID_ESC);
    }
    else if(key == SCANCODE_ENTER)
    {
        OnBtnCmd(hWnd, IDC_EDIT1);
    }
}
/*
*
*/
static int PaymentWinProc(HWND hWnd, int message, WPARAM wParam, LPARAM lParam)
{
    switch(message)
    {
    case MSG_CREATE:
        {
            CreatePaymentInterface(hWnd);
        }
        break;
    case MSG_PAINT:
        {
            HDC hdc = GetClientDC(hWnd);
            RECT rect;
            GetClientRect(hWnd, &rect);
            rect.left += 100;
            rect.top += MASTER_TITLE_TOP;
            rect.right -= 100;
            rect.bottom = rect.top + MASTER_TITLE_HEIGHT - 1;
            SetBkMode(hdc, BM_TRANSPARENT);
            SetTextColor(hdc, COLOR_HQ_GREEN(hdc));
            DrawText(hdc, GetWindowCaption (hWnd), -1, &rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
            ReleaseDC(hdc);
        }
        break;
    case MSG_CLOSE:
        {
            HWND hHosting = GetHosting(hWnd);
            if(hHosting > 0)
            {
                EnableWindow(hHosting, TRUE);
            }
            DestroyMainWindow(hWnd);
            MainWindowCleanup(hWnd);
        }
        return 0;
    case MSG_DESTROY:
        {
            ShowWindow(hWnd, SW_HIDE);
            DestroyAllControls(hWnd);
        }
        break;
    case MSG_KEYDOWN:
        {
            OnKeyDown(hWnd, wParam);
        }
        return 0;
    case MSG_ERASEBKGND:
        {
            DrawBitmapBkGnd(hWnd, (RECT*)lParam, &g_bmp[BMP_RAB]);
        }
        return 0;
    case MSG_COMMAND:
        {
            OnBtnCmd(hWnd, LOWORD(wParam));
        }
        return 0;
    case MSG_SETDATA:
        {
            const char *psc = (char *)wParam;
            if(SetPaymentID(g_knit_data, psc, strlen(psc) + 1))
            {
                psc = GetString(796);
            }
            else
            {
                psc = GetString(795);
                FlushPaymentWindowID(hWnd);
            }
            AfxInfoBox(hWnd, NULL, psc);
        }
        return 0;
    }
    return DefaultMainWinProc(hWnd, message, wParam, lParam);
}
/*
 *
 */
HWND CreatePaymentWindow(HWND hHosting)
{
    int x = (CX_SCREEN - CX_RAB_BMP) / 2;
    int y = (CY_SCREEN - CY_RAB_BMP) / 2;

    MAINWINCREATE CreateInfo;
    CreateInfo.dwStyle = WS_VISIBLE;
    CreateInfo.dwExStyle = WS_EX_NONE;
    CreateInfo.spCaption = GetString(787);
    CreateInfo.hMenu = 0;
    CreateInfo.hCursor = GetSystemCursor(0);
    CreateInfo.hIcon = 0;
    CreateInfo.MainWindowProc = PaymentWinProc;
    CreateInfo.lx = x;
    CreateInfo.ty = y;
    CreateInfo.rx = x + CX_RAB_BMP;
    CreateInfo.by = y + CY_RAB_BMP;
    CreateInfo.iBkColor = COLOR_lightwhite;
    CreateInfo.dwAddData = 0;
    CreateInfo.hHosting = hHosting;
    HWND hWnd = CreateMainWindow(&CreateInfo);
    if(hWnd > 0)
    {
        EnableWindow(hHosting, FALSE);
        SetFocusChild(GetDlgItem(hWnd, ID_EDIT));
        ShowWindow(hWnd, SW_SHOWNORMAL);
    }
    return hWnd;
}
